﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{
    public partial class Contact : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ( ! IsPostBack)
            {
                FillDate();
            }
        }

        private void FillDate()
        {
            var sql = "select uid ,username ,password,remarks from users";
            var dt = Dbhelper.GetDatatable(sql);
            GridView1.DataSource = dt;
            GridView1.DataBind();

        }
        //取消模式
        protected void GridView1_RowCancelingEdit1 (object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            FillDate();
        }
        //疑难点
        private string  GetCellVaule (int rowIndex ,int CelIndex)
        {
            var control = GridView1.Rows[rowIndex].Cells[CelIndex];
            var res = control.Controls.Count > 0 ? ((TextBox)control.Controls[0]).Text : control.Text;
            return res;
        }
        //删除模式
        protected void GridView1_RowDeleting1(object sender, GridViewDeleteEventArgs e)
        {
           
            var uid = GetCellVaule(e.RowIndex, 0);
            var sql = string.Format("delete Users where uid = '{0}'", uid);
            Dbhelper.AddOrDeleteOrUpdate(sql);
            FillDate();

        }
        //更新模式
        protected void GridView1_RowUpdating1 (object sender, GridViewUpdateEventArgs e)
        {
            var uid = GetCellVaule(e.RowIndex, 0);
            var isok = int.TryParse(uid, out int id);
            //转换成功为ture
            var UserName = GetCellVaule(e.RowIndex, 1);
            var password = GetCellVaule(e.RowIndex, 2);
            var remarks = GetCellVaule(e.RowIndex, 3);
            if (isok)
            { 
                //表示id存在是为更新 
                var sql1 = string.Format("update Users set UserName = '{0}',password = '{1}',remarks = '{2}' where uid = {3} ", UserName, password, remarks, uid);
                Dbhelper.AddOrDeleteOrUpdate(sql1);

            }
            else
            {   //表示id不存在是为添加
                var sql2 = string.Format("insert into users (username,password ,remarks ) values ('{0}','{1}','{2}') ", UserName, password, remarks);
                Dbhelper.AddOrDeleteOrUpdate(sql2);

            }


            GridView1.EditIndex = -1;
            FillDate();
        }
        //编辑模式
        protected void GridView1_RowEditing1(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            FillDate();
        }
        //添加按钮
        protected void Button1_Click1(object sender, EventArgs e)
        {
            //DataTable dt = new DataTable();
            //dt.Columns.Add(new DataColumn("uId"));
            //dt.Columns.Add(new DataColumn("Username"));
            //dt.Columns.Add(new DataColumn("Password"));
            //dt.Columns.Add(new DataColumn("Remarks"));

            //foreach (GridViewRow row in GridView1 .Rows)
            //{
            //    var newRow = dt.NewRow();

            //    newRow["uId"] = GetCellVaule(row.RowIndex, 0).Replace("&nbsp;","") ;
            //    newRow["Username"] = GetCellVaule(row.RowIndex, 1).Replace("&nbsp;", "");
            //    newRow["Password"] = GetCellVaule(row.RowIndex, 2).Replace("&nbsp;", "");
            //    newRow["Remarks"] = GetCellVaule(row.RowIndex, 3).Replace("&nbsp;", "");

            //    dt.Rows.Add(newRow);
            //}

            var sql = "select * from users";
            var tempdt = Dbhelper.GetDatatable(sql);

            var dt = tempdt.Copy(); 

            dt.Rows.Add(dt.NewRow());

            GridView1.DataSource = dt;
            GridView1.DataBind();

            GridView1.EditIndex = dt.Rows.Count - 1;

            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

    }

       
 }
